package com.yygo.util;

import com.yygo.configentity.view.Role;
import com.yygo.configentity.view.RoleView;
import com.yygo.configentity.view.RoleViews;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;

/**
 * Created by huanli on 2015/11/12.
 */
public class RoleViewConfigUtil {
    private static Log log = LogFactory.getLog(RoleViewConfigUtil.class);
    public RoleViews read(){
        RoleViews roleViews = new RoleViews();
        SAXReader saxReader= new SAXReader();
        Document document = null;
        try{
            URL xmlPath = this.getClass().getClassLoader().getResource("RoleView.xml");
            document = saxReader.read(xmlPath);
            Element root = document.getRootElement();
            List<Element> childList = root.elements();
            List<Role> roles = new ArrayList<>();
            for (Element childNode:childList) {
                Role role = new Role();
                String name = childNode.valueOf("@name" );
                role.setName(name);
                List<RoleView> roleViewList = new ArrayList<>();
                Element tmp = childNode.element("Views");
                for(Object properties: tmp.elements("View")) {
                    RoleView roleView = new RoleView();
                    String phaseName = ((Node) properties).valueOf("@phase");
                    String viewName = ((Node) properties).valueOf("@view");
                    boolean dealerCommentVisible = ((Node) properties).valueOf("@dealerCommentVisible").equals("true")? true:false;
                    boolean auditCommentVisible = ((Node) properties).valueOf("@auditCommentVisible").equals("true")? true:false;
                    boolean talkRecordVisible = ((Node) properties).valueOf("@talkRecordVisible").equals("true")? true:false;
                    boolean feedbackVisible = ((Node) properties).valueOf("@feedbackVisible").equals("true")? true:false;
                    boolean caAuditVisible = ((Node) properties).valueOf("@caAuditVisible").equals("true")? true:false;
                    boolean vehicleInfoVisible = ((Node) properties).valueOf("@vehicleInfoVisible").equals("true")? true:false;
                    boolean ruleVisible = ((Node) properties).valueOf("@ruleVisible").equals("true")? true:false;
                    boolean scoreVisible = ((Node) properties).valueOf("@scoreVisible").equals("true")? true:false;
                    roleView.setPhase(phaseName);
                    roleView.setView(viewName);
                    roleView.setAuditCommentVisible(auditCommentVisible);
                    roleView.setCaAuditVisible(caAuditVisible);
                    roleView.setDealerCommentVisible(dealerCommentVisible);
                    roleView.setFeedbackVisible(feedbackVisible);
                    roleView.setTalkRecordVisible(talkRecordVisible);
                    roleView.setVehicleInfoVisible(vehicleInfoVisible);
                    roleView.setRuleVisible(ruleVisible);
                    roleView.setScoreVisible(scoreVisible);
                    roleViewList.add(roleView);
                }
                role.setRoleViews(roleViewList);
                roles.add(role);
            }
            roleViews.setRoles(roles);
        }
        catch(Exception e){
            log.trace(e.getMessage());
        }
        return roleViews;
    }
}
